package com.yunhe.authority.repository.authority;

import com.yunhe.authority.domain.authority.BackUser;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author zhengbo
 * @date 2021/10/21 15:17
 */
@Repository
public interface BackUserRepository extends Neo4jRepository<BackUser, Long> {

    /**
     * 修改密码
     * @param id 用户ID
     * @param password 用户密码
     */
    @Query("MATCH (bu:BackUser) WHERE ID(bu)={id} SET bu.password={password} return bu")
    BackUser updatePassword(@Param("id") Long id, @Param("password") String password);

    /**
     * 根据用户名密码查询后台账号
     * @param name 用户名
     * @param password 用户密码
     */
    @Query("MATCH (bu:BackUser) WHERE bu.name={name} and bu.password={password} return bu")
    BackUser findByNameAndPassword(@Param("name") String name, @Param("password") String password);

    /**
     * 根据手机号密码查询后台账号
     * @param phone 手机号
     * @param password 用户密码
     */
    @Query("MATCH (bu:BackUser) WHERE bu.phone={phone} and bu.password={password} return bu")
    BackUser findByPhoneAndPassword(@Param("phone") String phone, @Param("password") String password);

    /**
     * 根据用户名查询后台账号
     * @param name 用户名
     */
    @Query("MATCH (bu:BackUser) WHERE bu.name={name} return bu limit 1")
    BackUser findByName(@Param("name") String name);

    /**
     * 根据手机号查询后台账号
     * @param phone 手机号
     */
    @Query("MATCH (bu:BackUser) WHERE bu.phone={phone} return bu limit 1")
    BackUser findByPhone(@Param("phone") String phone);

    /**
     * 后台用户与后台角色绑定
     * @param userId
     * @param roleIds
     */
    @Query("MATCH (bu:BackUser), (r:Role) where ID(bu)={userId} AND ID(r) IN {roleIds} CREATE (bu)-[:HAS_ROLE]->(r)")
    void bindBackRole(@Param("userId") Long userId, @Param("roleIds") List<Long> roleIds);
}
